package com.gimi.cloud.bbp.manage.consumer.service.page.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.page.PageModelDTO;
import com.gimi.cloud.bbp.manage.consumer.feign.page.PageModelFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.page.PageModelService;

@Service
public class PageModelServiceImpl implements PageModelService{
	
	@Autowired private PageModelFeignService pageModelFeignService;
	
	@Override
	public Response getList(String requestBody, HttpServletRequest request, HttpServletResponse response) {
		PageModelDTO dto = JSON.toJavaObject(JSON.parseObject(requestBody), PageModelDTO.class);
		return pageModelFeignService.getList(dto);
	}
	
	@Override
	public Response delInfo(String requestBody, HttpServletRequest request, HttpServletResponse response) {
		PageModelDTO dto = JSON.toJavaObject(JSON.parseObject(requestBody), PageModelDTO.class);
		if (dto.getIds() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return pageModelFeignService.delInfo(dto);
	}
	
	@Override
	public Response saveInfo(String requestBody, HttpServletRequest request, HttpServletResponse response) {
		PageModelDTO dto = JSON.toJavaObject(JSON.parseObject(requestBody), PageModelDTO.class);
		if (dto.getPageId() == null && dto.getProdId() == null && dto.getUrl() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		if (dto.getType() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return pageModelFeignService.saveInfo(dto);
	}

	@Override
	public Response getInfoById(String requestData, HttpServletRequest request, HttpServletResponse response) {
		PageModelDTO dto = JSON.toJavaObject(JSON.parseObject(requestData), PageModelDTO.class);
		if (dto.getId() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return pageModelFeignService.getInfoById(dto);
	}


}
